package com.hl.component.exception.handle;

import android.util.Log;
import net.tsz.afinal.exception.DbException;
import com.heliang.common.FileLog;
import com.heliang.util.Constants;
import com.hl.component.exception.ActionException;
import com.hl.component.exception.BaseException;
import com.hl.component.exception.DaoException;
import com.hl.component.exception.DataException;
import com.hl.component.exception.ErrorCode;
import com.hl.component.exception.ServiceException;

/**
 * Title: ExceptionHandle <br>
 * Description: 异常处理类，负责抛出各类异常，获取异常信息。 <br>
 * CreateTime: 2012-08-07 16:39 <br>
 * Copyright: Copyright (c) 2012 taiheiot.com<br>
 * @author heliang
 * @since 1.0
 */
public class ExceptionHandle {

    /**
     * 根据抛出BaseException
     * @param e 捕获的异常
     * @param errorCode 异常编码
     * @param classAndMethod 捕获到异常的类名和方法名
     * @throws BaseException
     */
    public static void throwBaseException(Exception e, String errorCode, String classAndMethod)
        throws BaseException {
        Log.e(Constants.TAG, errorCode + ":" + classAndMethod);
        if (e.getCause() != null) {
            Log.e(Constants.TAG, e.getCause().getMessage());
            FileLog.log(errorCode + ":" + classAndMethod + e.getCause().getMessage());
        } else if (e.getMessage() != null) {
            Log.e(Constants.TAG, e.getMessage());
            FileLog.log(errorCode + ":" + e.getMessage());
        }

        // 打印异常堆栈
        e.printStackTrace();

        // 抛出新封装的异常
        if (errorCode != null) {
            throw new BaseException(errorCode);
        } else {
            throw new BaseException(ErrorCode.BASE_ERROR);
        }
    }

    /**
     * 根据抛出DaoException
     * @param e 捕获的异常
     * @param errorCode 异常编码
     * @param classAndMethod 捕获到异常的类名和方法名
     * @throws DaoException
     */
    public static void throwDaoException(Exception e, String errorCode, String classAndMethod)
        throws DaoException {
        Log.e(Constants.TAG, errorCode + ":" + classAndMethod);
        if (e.getCause() != null) {
            Log.e(Constants.TAG, e.getCause().getMessage());
            FileLog.log(errorCode + ":" + classAndMethod + e.getCause().getMessage());
        } else if (e.getMessage() != null) {
            Log.e(Constants.TAG, classAndMethod + e.getMessage());
            FileLog.log(errorCode + ":" + e.getMessage());
        }

        // 打印异常堆栈
        e.printStackTrace();

        // 抛出新封装的异常
        if (errorCode != null) {
            throw new DaoException(errorCode);
        } else {
            throw new DaoException(ErrorCode.DAO_ERROR);
        }
    }

    /**
     * 根据抛出ServiceException
     * @param e 捕获的异常
     * @param errorCode 异常编码
     * @param classAndMethod 捕获到异常的类名和方法名
     * @throws ServiceException
     */
    public static void throwServiceException(Exception e, String errorCode, String classAndMethod)
        throws ServiceException {
        Log.e(Constants.TAG, errorCode + ":" + classAndMethod);
        if (e.getCause() != null) {
            Log.e(Constants.TAG, e.getCause().getMessage());
            FileLog.log(errorCode + ":" + classAndMethod + e.getCause().getMessage());
        } else if (e.getMessage() != null) {
            Log.e(Constants.TAG, classAndMethod + e.getMessage());
            FileLog.log(errorCode + ":" + e.getMessage());
        }

        // 打印异常堆栈
        e.printStackTrace();

        // 抛出新封装的异常
        if (errorCode != null) {
            throw new ServiceException(errorCode);
        } else {
            throw new ServiceException(ErrorCode.SERVICE_ERROR);
        }
    }

    /**
     * 根据抛出DataException
     * @param e 捕获的异常
     * @param errorCode 异常编码
     * @param classAndMethod 捕获到异常的类名和方法名
     * @throws DataException
     */
    public static void throwDataException(Exception e, String errorCode, String classAndMethod)
        throws DataException {
        Log.e(Constants.TAG, errorCode + ":" + classAndMethod);
        if (e.getCause() != null) {
            Log.e(Constants.TAG, e.getCause().getMessage());
            FileLog.log(errorCode + ":" + classAndMethod + e.getCause().getMessage());
        } else if (e.getMessage() != null) {
            Log.e(Constants.TAG, classAndMethod + e.getMessage());
            FileLog.log(errorCode + ":" + e.getMessage());
        }

        // 打印异常堆栈
        e.printStackTrace();

        // 抛出新封装的异常
        if (errorCode != null) {
            throw new DataException(errorCode);
        } else {
            throw new DataException(ErrorCode.DATA_ERROR);
        }
    }

    /**
     * 根据抛出ActionException
     * @param e 捕获的异常
     * @param errorCode 异常编码
     * @param classAndMethod 捕获到异常的类名和方法名
     */
    public static ActionException getActionException(Exception e, String errorCode, String classAndMethod) {
        Log.e(Constants.TAG, errorCode + ":" + classAndMethod);
        if (e.getCause() != null) {
            Log.e(Constants.TAG, e.getCause().getMessage());
            FileLog.log(errorCode + ":" + classAndMethod + e.getCause().getMessage());
        } else if (e.getMessage() != null) {
            Log.e(Constants.TAG, classAndMethod + e.getMessage());
            FileLog.log(errorCode + ":" + e.getMessage());
        }

        // 打印异常堆栈
        e.printStackTrace();

        // 抛出新封装的异常
        if (errorCode != null) {
            return new ActionException(errorCode);
        } else {
            return new ActionException(ErrorCode.ACTION_ERROR);
        }
    }

}
